﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DNA.Mvc.jQuery;

namespace DNA.Mvc.Publishing
{
    public class CategoryNodeProvider : HierarchicalNodeProviderBase
    {
        private ICategoryRepository _repository;
        private HierarchicalNode _root;

        public CategoryNodeProvider(ICategoryRepository repository)
        {
            _repository = repository;
        }

        public override IEnumerable<HierarchicalNode> GetChildNodes(HierarchicalNode node)
        {
            if (node == null)
                throw new ArgumentNullException("node");
            int id = int.Parse(node.Key);

            var _child = _repository.Children(id);
            var nodes = new List<HierarchicalNode>();
            foreach (var _cat in _child)
            {
                var childrenNode = PopulateNodeForCategory(_cat);
                if (childrenNode != null)
                    nodes.Add(childrenNode);
            }
            return nodes;
        }

        public override HierarchicalNode GetParentNode(HierarchicalNode node)
        {
            var cat = node.Item as Category;
//            int id = int.Parse(node.Key);

            if ((cat == null) || (cat.ParentID == 0) || node.Equals(RootNode))
                return null;

            if (cat.ParentID.ToString() == RootNode.Key)
                return RootNode;

            return PopulateNodeForCategory(_repository.Find(cat.ParentID));
        }

        public override HierarchicalNode RootNode
        {
            get
            {
                if (_root == null)
                {
                    var web = WebSiteContext.Current.Web;
                    var cat = _repository.Find(web.Name);

                    if (cat == null)
                    {
                        cat = new Category()
                        {
                            Title = web.Name,
                            Description = web.Description,
                            LastPosted = new DateTime(1800, 1, 1),
                            Path = web.Name
                        };
                        this._repository.Create(cat);
                        this._repository.Submit();
                    }

                    _root = new HierarchicalNode(this)
                    {
                        Title = cat.Title,
                        Description = cat.Description,
                        Key = cat.ID.ToString(),
                        Item = cat,
                        Attributes = ObjectHelper.ConvertObjectToDictionary(new { key = cat.ID.ToString() })
                    };
                    _root.Title = web.Title;
                 }
                return _root;
            }
            set
            {
                _root = value;
            }
        }

        public override HierarchicalNode FindNodeFormKey(string key)
        {
            if (string.IsNullOrEmpty(key))
                throw new ArgumentNullException("key");
            int id = int.Parse(key);
            return PopulateNodeForCategory(_repository.Find(id));
        }

        private HierarchicalNode PopulateNodeForCategory(Category cat)
        {
            if (cat != null)
            {
                var _node = new HierarchicalNode(this)
                {
                    Title = cat.Title,
                    Description = cat.Description,
                    Key = cat.ID.ToString(),
                    Item = cat,
                    Attributes = ObjectHelper.ConvertObjectToDictionary(new { key = cat.ID.ToString() })
                };
                return _node;
            }
            return null;
        }
    }
}
